<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>  
<!DOCTYPE html>
<html>
<head>
<title>机构管理列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="decorator" content="default" />

    
<%@ include file="/WEB-INF/views/include/bootstrap-table.jsp"%>

<style type="text/css">
.date-field-class{
  width: 100px;
}

</style>
    
</head>

<body class="console-frame-body">
<div class="layui-fluid">
    <div class="layui-row">
        <blockquote class="layui-elem-quote news_search">
        <form id="searchForm">
		    <sys:message content="${message}"/>
		    <input id="id" type="hidden" name="id" value="${param.id}">
			<input id="parentIds" type="hidden" name="parentIds" value="${param.parentIds}">
			<input id="myParentId" type="hidden" name="myParentId" value="${param.myParentId}">
			<!-- 是否显示登录机构所在部门 -->
			<input id="showMyOffice" type="hidden" name="showMyOffice" value="false">
			<!-- 是否查询父机构 左边点击的时候是否一起查询出来 -->
			<input id="includeParentId" type="hidden" name="includeParentId" value="${param.includeParentId}">
				
            <div class="layui-inline">
		           <div class="layui-inline"> 
		                                   机构名称：
		              <div class="layui-input-inline">
		                   <input id="name" name="name"  class="layui-input search_input" type="text">
		              </div>
		                                    机构代码：
		              <div class="layui-input-inline">
		                  <input id="code" name="code"  class="layui-input search_input" type="text">
		              </div>
                <a  id="btn_search" class="layui-btn search_btn">查询</a>
                <a  id="btn_reset" onclick="formResetBySearch();" class="layui-btn layui-btn-primary search_btn">重置</a>
            </div>
           </form>
        </blockquote>

		   <div id="toolbar">
		      <div class="layui-btn-group">
				  <shiro:hasPermission name="sys:office:create">
					  <button type="button" class="layui-btn layui-btn-normal" onclick="gotoView('${ctx}/sys/office/form');">新增</button>
				      <button type="button" class="layui-btn layui-btn-normal" onclick="addLowerOffice('${ctx}/sys/office/form',false)">添加下级组织</button>
					  <button type="button" class="layui-btn layui-btn-normal" onclick="addpeer('${ctx}/sys/office/form',false)">添加同级组织</button>
				  </shiro:hasPermission>
				  <shiro:hasPermission name="sys:office:delete">
					  <button type="button" class="layui-btn layui-btn-danger" onclick="batchDelete('sys/office/batchDelete')">批量删除</button>
				 </shiro:hasPermission>
	       </div>
		</div>

	    <table id="table" 
	         data-toggle="table" 
	         data-url="${ctx}/sys/office/officeListJson"
	         data-toolbar="#toolbar"
		     data-height="${dataHeight410}" 
		     data-maintain-selected="true" 
		     data-side-pagination="server"
		     data-click-to-select="true" 
		     data-pagination="true" 
		     data-single-select="false"
		     data-id-field="id"
		     data-query-params="doQuery"
		     data-cache="false"
		     data-show-columns="true"
		     data-page-list="${dataPageList}">
		<thead>
			<tr>
				<th data-checkbox="true"></th>
				<th data-field="id" data-formatter="operationFormatter" class="date-field-class">操作</th>
				<th data-field="name" data-sortable="true">机构名称</th>
				<th data-field="zipCode" data-sortable="true">机构编号</th>
				<th data-field="code" data-sortable="true">机构代码</th>
				<th data-field="type" data-formatter="typeFormatter" data-sortable="true">组织类型</th>
				<th data-field="remarks" data-visible="false">备注</th>
				<!-- data-sortable="true" 排序 -->
			    <!-- data-visible="false" 可以隐藏列 -->
			</tr>
		  </thead>
	    </table>
    </div>
</div>

	<script type="text/javascript">
	  var $table = $('#table');
	  $(function () { 
          
	      $("#btn_search").click(function () { 
	              $table.bootstrapTable('refresh'); 
	       }); 
	   }); 
	  
	  //查询函数
	  function doQuery(params) {
		    var name_value = $("#name").val();
			var code_value = $("#code").val();
			 
			if(name_value != null && name_value !=''){
				params.name = name_value;
			}
			if(code_value != null && code_value !=''){
				params.code = code_value;
			}
			
			var parentIds = $("#parentIds").val();
			if(parentIds != null && parentIds !=''){
				params.parentIds = parentIds;
			}
			
			var showMyOffice = $("#showMyOffice").val();
			if(showMyOffice != null && showMyOffice !=''){
				params.showMyOffice = showMyOffice;
			}
			
			
			var myParentId = $("#myParentId").val();
			if(myParentId != null && myParentId !=''){
				params.myParentId = myParentId;
			}
			
			var includeParentId = $("#includeParentId").val();
			if(includeParentId != null && includeParentId !=''){
				params.includeParentId = includeParentId;
			}
		    return params;
	}
	  
	//添加下级组织机构  add for lgx 20170519
	function addLowerOffice(url, istrue){
			var isclose=istrue==true?true:false;
			var row_date_arr = $table.bootstrapTable('getSelections');
			
			if(row_date_arr.length > 1){
				showTip("只可以选择一条数据！", "info", undefined, undefined);
			    return false;
			}
			if(row_date_arr.length == 0){
				showTip("请选择一条数据！", "info", undefined, undefined);
			    return false;
			}
			
			var id = '';
			var grade;
			$.each(row_date_arr,function (index,row){
				id = row.id;
				grade = row.grade;
			});
			
			if(grade > 3) {
				top.$.jBox.tip("该组织机构不允许添加下级组织",""); 
				return false;
			}
			
			url=url+"?parent.id="+id+"&grade=" + (parseInt(grade) + 1);
			//opencreateWindow(url,'添加组织机构',1000,600,'10px',istrue);
			gotoView(url);
	}
		
    //添加同级别
	function addpeer(url, istrue) {
				var isclose=istrue==true?true:false;
				var row_date_arr = $table.bootstrapTable('getSelections');
				
				if(row_date_arr.length > 1){
					showTip("只可以选择一条数据！", "info", undefined, undefined);
				    return false;
				}
				if(row_date_arr.length == 0){
					showTip("请选择一条数据！", "info", undefined, undefined);
				    return false;
				}
				
				var param = '';
				$.each(row_date_arr,function (index,row){
					param = row.id;
				});
				
				url=url+"?id="+param+"&someLeave=true";
				//opencreateWindow(url,'添加组织机构',1000,600,'10px',istrue);
				gotoView(url);
	}
		
	function addNextOffice(url, istrue) {
			var isclose=istrue==true?true:false;
			var checkboxs=$(".tbbody").find("input[type=checkbox]:checked");
			if(checkboxs.length>1){
				top.$.jBox.tip("不能多条数据同时修改",""); 
				return false;
			}
			if(checkboxs.length==0){
				top.$.jBox.tip("请先选择一条组织机构记录",""); 
				return false;
			}
			
			//获取选中的tr
			var tr = $(".tbbody").find("input[type=checkbox]:checked").parents("tr");
			var grade = tr.find("#grade").val();
			var id = tr.find("#id").val();
			if(grade > 3) {
				top.$.jBox.tip("该组织机构不允许添加下级组织",""); 
				return false;
			}
			
			url=url+"?parent.id="+id+"&grade=" + (parseInt(grade) + 1);
			//opencreateWindow(url,'添加下级组织',1000,600,'10px',isclose);
			gotoView(url);
	}
	
	/**
	*  操作项  add for lgx  提供查看，修改，删除按钮，如果还有系统按钮，
	*  比如禁用，可在子页面进行自定义函数添加（建议列表操作项按钮不要超过5个）
	*/
	var hasView = '${fns:hasPermission("sys:office:view")}';
	var hasEdit = '${fns:hasPermission("sys:office:edit")}';
	var hasDelete = '${fns:hasPermission("sys:office:delete")}';
	function operationFormatter(value, row){
		
		var params = "id="+value;
		//查看
		//var html = '<a href="javascript:void(0);" style="padding-right:5px;" onclick="detail(\'sys/role/roleDetail\',\'查看用户\',\''+params+'\')">查看</a>';
		//修改
		var html = '';
		
	    if(hasEdit == 'true'){
	    	html = html + '<a href="javascript:void(0);" class="layui-btn layui-btn-xs" style="padding-right:5px;" onclick="gotoView(\'${ctx}/sys/office/form?'+params+'\');">修改</a>';
		}else{
			html = html + ' <button class="layui-btn layui-btn-disabled layui-btn-xs">修改</button>';
		} 

	    if(hasDelete == 'true'){
			//删除
			html= html +'<a href="javascript:void(0);" class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteData(\'sys/office/deleteAjax\',\'是否删除数据？\',\''+params+'\')">删除</a>';
	    }else{
	    	html = html + '<button class="layui-btn layui-btn-disabled layui-btn-xs">删除</button>';
	    }

		return html;
	}  
	
	function typeFormatter(value, row){
		   var data = ${fns:getDictListJson('sys_office_type')}; 
		   value = getDictLabel(data,value,'-');
		   return value;
	}
	</script>
	
	<jsp:include page="/WEB-INF/views/include/pageJs.jsp" />

</body>
</html>